package managers;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import managers.messengerManager.MessengerManager;
import managers.messengerManager.services.SendSupportDocumentService;
import managers.messengerManager.services.Service;

public class SupportManager {


	private static SupportManager sharedInstance = new SupportManager();

	private SupportManager () {

	}


	public void sendBugReport (String d1, String d2, String d3) throws Exception {
		File temp = File.createTempFile("support", ".tmp");
		temp.deleteOnExit();

		File stackTraceFile = new File ("C:/ProgramData/SpotMessenger/stackTrace.txt");

		BufferedWriter out = new BufferedWriter(new FileWriter(temp, true));
		out.write("BUG REPORT, SENT BY: "+MessengerManager.getSharedInstance().getDataManager().getUser(MessengerManager.getSharedInstance().getUserId()));
		out.newLine();
		out.write("DATE: "+d1);
		out.newLine();
		out.write("WHAT WAS HE DOING: "+d2);
		out.newLine();
		out.write("WHAT HAPPENED: "+d3);
		out.newLine();
		
		if (stackTraceFile.exists()) {
			out.newLine();
			out.write("############## STACK TRACE ##############");
			out.newLine();
			BufferedReader in = new BufferedReader(new FileReader (stackTraceFile));
			String line;
			while ((line = in.readLine()) != null) {
				out.write (line);
				out.newLine();
			}
			in.close();
		}
		
		out.close();
		
		sendSupportDocument(temp);
	}

	public void sendComment (String comment) throws Exception {
		File temp = File.createTempFile("support", ".tmp");
		temp.deleteOnExit();

		BufferedWriter out = new BufferedWriter(new FileWriter(temp));
		out.write("SPOT MESSENGER COMMENT, SENT BY: "+MessengerManager.getSharedInstance().getDataManager().getUser(MessengerManager.getSharedInstance().getUserId()));
		out.newLine();
		out.write(comment);
		out.close();

		sendSupportDocument(temp);
	}

	private void sendSupportDocument (File f) {
		Service service = new SendSupportDocumentService(f);
		MessengerManager.getSharedInstance().addService(service);
		service.start();
	}

	public static SupportManager getSharedInstance() {
		return sharedInstance;
	}



}
